package com.atguigu.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-06-27 18:42
 */
public class CompletableFutureAPIThenCombine {
  public static void main(String[] args) {
      CompletableFuture<Integer> supplyAsync1 = CompletableFuture.supplyAsync(() -> {
          System.out.println(Thread.currentThread().getName() + "\t ---supplyAsync1启动");
          try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
          return 10;
      });
      CompletableFuture<Integer> supplyAsync2 = CompletableFuture.supplyAsync(() -> {
          System.out.println(Thread.currentThread().getName() + "\t ---supplyAsync2启动");
          try { TimeUnit.MILLISECONDS.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}
          return 20;
      });

      CompletableFuture<Integer> thenCombine = supplyAsync1.thenCombine(supplyAsync2, (x, y) -> {
          System.out.println("开始为两个结果合并");
          return x + y;
      });

    System.out.println(thenCombine.join());
  }
}
